<?php

namespace app\common\model\shop;

use app\common\model\shop\goods\Acct;
use think\Model;


class Goodsstockin extends Model
{





    // 表名
    protected $name = 'shop_goodsstockin';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];










    public function acct()
    {
        return $this->belongsTo('app\common\model\Acct', 'acct_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    protected static function init()
    {
        self::beforeInsert(function ($row) {
            $auth = new \app\admin\library\Auth();
            $row->admin_id = $auth->isLogin() ? $auth->id : 0;

            $has = Goods::where('id', $row->goods_id)->find();
            if (!$has) {
                exception('商品不存在，请先授权商品');
            }
            $has->stock += $row->num;
            $has->save();
        });
    }
}
